package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.ModuleContentItem.MODULE_CONTENT_ITEM;

import java.util.List;

import org.jooq.impl.TableImpl;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.entity.family.ModuleContentItem;
import com.tuanzi.loan.business.repository.family.custom.ModuleContentItemRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.view.family.ActivityModelView.ContentItem;

@Repository
public class ModuleContentItemRepositoryImpl extends JOOQBaseRepositoryPlus implements ModuleContentItemRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return MODULE_CONTENT_ITEM;
	}

	@Override
	public List<ModuleContentItem> findAllByModelId(Long modelId) {
		return dslContext.select(MODULE_CONTENT_ITEM.fields()).from(MODULE_CONTENT_ITEM).where(MODULE_CONTENT_ITEM.MODELID.eq(modelId)).orderBy(MODULE_CONTENT_ITEM.SORT)
				.fetch(record -> {
					ModuleContentItem item = new ModuleContentItem();
					item.setId(record.get(MODULE_CONTENT_ITEM.ID));
					item.setName(record.get(MODULE_CONTENT_ITEM.NAME));
					item.setContent(record.get(MODULE_CONTENT_ITEM.CONTENT));
					item.setSort(record.get(MODULE_CONTENT_ITEM.SORT));
					item.setType(record.get(MODULE_CONTENT_ITEM.TYPE));
					item.setRemark(record.get(MODULE_CONTENT_ITEM.REMARK));
					item.setModelId(record.get(MODULE_CONTENT_ITEM.MODELID));
					return item;
				});
	}

	@Override
	public void deleteByModelId(Long modelId) {
		dslContext.delete(MODULE_CONTENT_ITEM).where(MODULE_CONTENT_ITEM.MODELID.eq(modelId)).execute();
	}

	@Override
	public Integer save(Long modelId, ContentItem contentItem) {
		return dslContext
				.insertInto(MODULE_CONTENT_ITEM, MODULE_CONTENT_ITEM.CONTENT, MODULE_CONTENT_ITEM.NAME, MODULE_CONTENT_ITEM.REMARK, MODULE_CONTENT_ITEM.SORT,
						MODULE_CONTENT_ITEM.TYPE, MODULE_CONTENT_ITEM.MODELID)
				.values(contentItem.getContent(), contentItem.getName(), contentItem.getRemark(), contentItem.getSort(), contentItem.getType(), modelId).execute();
	}
}
